let Product = require('../../db/productSchma');
// 引入form表单解析中间件
let formidable = require('formidable');
let path = require('path');

let form = formidable({
    keepExtensions: true,
    uploadDir: path.join(__dirname, '../', '../', 'public', 'uploads')
});
module.exports = (req, res) => {

    // console.log(req.body);

    form.parse(req, async(err, fields, files) => {

        // console.log(files);
        // console.log(fields);

        if (err) {
            res.status(500).send(err);
        } else {
            // 创建商品信息添加到数据库中
            let addRel = await Product.create({
                title: fields.title,
                cate_id: fields.cate_id,
                goods_id: fields.goods_id,
                price: fields.price,
                fee: fields.fee,
                description: fields.description,
                pic: files.pic.filepath.split("public")[1]
            });
            if (addRel) {
                // res.redirect('/admin/userlist');
                res.redirect('/product/productlist');
            } else {
                res.status(500).send('添加失败');
            }

        };
    });
}